//
//  COKVATrackerManager.m
//  CocoProject
//
//  Created by Vill on 2023/11/3.
//

#import "COKVATrackerManager.h"
@import KochavaTracker;

@implementation COKVATrackerManager

+ (instancetype)shared{
    static dispatch_once_t once;
    static COKVATrackerManager *instance;
    dispatch_once(&once, ^{
        instance = [[COKVATrackerManager alloc] init];
    });
    return instance;
}

- (void)kvaTrackerWith:(void(^)(NSString *str))block{
    // 归因
    if (!KVATracker.shared.attribution.result.retrievedBool){
         [KVATracker.shared.attribution retrieveResultWithCompletionHandler:^(KVAAttributionResult * _Nonnull attributionResult) {
             // do something with attributionResult
             if (attributionResult != nil) {
                 if (attributionResult.attributedBool) {
                      if (![self isNullStr:attributionResult.rawDictionary[@"site_id"]]) {
                          if (block != nil) {
                              block(attributionResult.rawDictionary[@"site_id"]);
                          }
                      }else{
                          if (block != nil) {
                              block(@"1");
                          }
                      }
                 }else{
                     if (block != nil) {
                         block(@"1");
                     }
                 }
             }else{
                 if (block != nil) {
                     block(@"1");
                 }
             }
         }];
     }else{
         KVAAttributionResult *result = KVATracker.shared.attribution.result;
         if (result != nil) {
             if (result.attributedBool) {
                 if (![self isNullStr:result.rawDictionary[@"site_id"]]) {
                     if (block != nil) {
                         block(result.rawDictionary[@"site_id"]);
                     }
                 }else{
                     if (block != nil) {
                         block(@"1");
                     }
                 }
             }else{
                 if (block != nil) {
                     block(@"1");
                 }
             }
         }else{
             if (block != nil) {
                 block(@"1");
             }
         }
     }
}

/**
 获取字符串的首字母, 缺省为#
 */
- (NSString*)firstChar:(NSString *)string{
    if ((string == nil)|| (string == NULL) || ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) || [string isKindOfClass:[NSNull class]]) {
        return @"#";
    }
    NSMutableString *source = [NSMutableString stringWithString:string];
    CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return [[source capitalizedString] substringToIndex:1];
}

//判断字符串是否为空
- (BOOL)isNullStr:(NSString *)string
{
    if (string == NULL || string == nil)
    {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
    {
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    return NO;
}

@end
